﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MarioMapEditor
{
    class TreeNode
    {
        public static int MIN_SIZE = 1600;
        public TreeNode(int left, int top, int right, int bot)
        {
            pos.left    = left;
            pos.top     = top;
            pos.right   = right;
            pos.bot     = bot;

            listObjects = new List<TreeObject>();
        }
        
        public string id;
        public AABB pos;

        public TreeNode northWest;
        public TreeNode northEast;
        public TreeNode southWest;
        public TreeNode southEast;

        public List<TreeObject> listObjects;

        public void build()
        {
            int tempx = pos.right - pos.left;
            int tempy = pos.top - pos.bot;

            if (this.listObjects.Count == 0) return;
            if (tempx <= MIN_SIZE || tempy <= MIN_SIZE)
            {
                return;
            }
            else
            {
                if (tempx % 2 != 0)
                {
                    tempx++;
                }

                if (tempy % 2 != 0)
                {
                    tempy++;
                }
            }

            int left, top, right, bot, w,h;
            w = tempx / 2;
            h = tempy / 2;

            left = pos.left; 
            top = pos.top; 
            right = pos.left + w; 
            bot = pos.bot + h;
            northWest = new TreeNode(left, top, right, bot);
            northWest.id = this.id + "1";

            left = pos.left + w; 
            top = pos.top; 
            right = pos.right;
            bot = pos.bot + h;
            northEast = new TreeNode(left, top, right, bot);
            northEast.id = this.id + "2";

            left = pos.left;
            top = pos.bot + h; 
            right = pos.left + w; 
            bot = pos.bot;
            southWest = new TreeNode(left, top, right, bot);
            southWest.id = this.id + "4";

            left = pos.left + w;
            top = pos.bot + h; 
            right = pos.right; 
            bot = pos.bot;
            southEast = new TreeNode(left, top, right, bot);
            southEast.id = this.id + "3";

            for (int i = 0; i < listObjects.Count; i++)
            {
                if (TreeClipping.checkCollision(listObjects[i].pos, northWest.pos))
                {
                    TreeObject ob;
                    ob.pos = TreeClipping.clipRECT(listObjects[i].pos, northWest.pos);
                    ob.target = listObjects[i].target;
                    northWest.listObjects.Add(ob);
                }

                if (TreeClipping.checkCollision(listObjects[i].pos, northEast.pos))
                {
                    TreeObject ob;
                    ob.pos = TreeClipping.clipRECT(listObjects[i].pos, northEast.pos);
                    ob.target = listObjects[i].target;
                    northEast.listObjects.Add(ob);
                }

                if (TreeClipping.checkCollision(listObjects[i].pos, southWest.pos))
                {
                    TreeObject ob;
                    ob.pos = TreeClipping.clipRECT(listObjects[i].pos, southWest.pos);
                    ob.target = listObjects[i].target;
                    southWest.listObjects.Add(ob);
                }

                if (TreeClipping.checkCollision(listObjects[i].pos, southEast.pos))
                {
                    TreeObject ob;
                    ob.pos = TreeClipping.clipRECT(listObjects[i].pos, southEast.pos);
                    ob.target = listObjects[i].target;
                    southEast.listObjects.Add(ob);
                }

            }

            listObjects.Clear();

            northWest.build();
            northEast.build();
            southWest.build();
            southEast.build();
            
        }
    }
}
